﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco;

namespace MartinsNeves
{
    public class Utils
    {
        private static ICacheProvider _Cache
        {
            get
            {
                if (System.Web.HttpContext.Current.Session != null)
                    return System.Web.HttpContext.Current.Session["MartinsNeves_UtilsCache"] != null ? (CacheProvider)System.Web.HttpContext.Current.Session["MartinsNeves_UtilsCache"] : null;

                return null;
            }
            set
            {
                System.Web.HttpContext.Current.Session.Add("MartinsNeves_UtilsCache", value);
            }
        }

        public static ICacheProvider Cache
        {
            get
            {
                if (_Cache == null)
                    _Cache = new CacheProvider();
                return _Cache;
            }
        }
       
        public static string GetImageUrl(int mediaId)
        {      
            string key = "imagem"+mediaId;
            if (Cache.IsSet(key))
                return Cache.Get(key).ToString();

            var media = new umbraco.cms.businesslogic.media.Media(mediaId);
            var file = media.getProperty("umbracoFile");
            Cache.Set(key, (string)file.Value, 60);
            return (string)file.Value;

        }
    }
}